(function () {
    /**
     * var confFont = {
     *     //height   : 26,
     *     //font     : "Microsoft Yahei",
     *     fontSize : 20,
     *     color    : "#f0f",
     *     valign   : "middle"
     * }
     * var notify = new com.notify(confFont, 200);
     * notify.width = 460;
     * notify.height = 30;
     * notify.pos(200, 100);
     * Laya.stage.addChild(notify);
     * notify.add("test notify");
     */
    var notifyLists = [];

    function Notify(font, speed, wrap){
        Notify.super(this);
        this._animation = false;
        this._UIInit    = false;

        wrap && this._init(wrap);
        this._createUI(font);

        this._speed = speed || 200;
    }
    Laya.class(Notify, "com.notify", Laya.Box);

    var _proto = Notify.prototype;
    _proto._init = function (wrap) {
        this._UIInit    = true;

        this.width      = wrap.width;
        this.height     = wrap.height;
        this.scrollRect = {x : 0, y : 0, width : wrap.width, height : wrap.height};
    }
    _proto._createUI = function (font) {
        var _text = new Laya.Label();
            _text.autoSize = true;
            _text.height   = font.height || this.height;
            _text.valign   = font.valign || "middle";

            font.fontSize  && (_text.fontSize = font.fontSize);
            font.color     && (_text.color    = font.color);
            font.font      && (_text.font     = font.font);

        this._text = _text;
        this.addChild(_text);
    }
    _proto._next = function (_msg) {
        if(!_msg && notifyLists.length == 0){
            console.log("公告等待列表为空");
            this._animation = false;
            return;
        }
        this._animation = true;

        var _wrapWidth = this.width;
        var _notify    = _msg || notifyLists.shift();
        console.log("本条公告:" + _notify);

        this._text.text = _notify
        this._text.x    = _wrapWidth;
        this._text.y    = 0;

        var _textWidth = this._text.width;
        var totalTime  = (_wrapWidth + _textWidth) / this._speed * 2000;

        Laya.Tween.to(this._text, {x : - _textWidth}, totalTime, null, Laya.Handler.create(this, function () {
            Laya.timer.once(2000, this, this._next);
        }));
    }
    _proto._addItem = function (msg) {
        if(this._animation){
            if(msg == this._text.text){return false;}

            for(var i in notifyLists){
                if(notifyLists[i] == msg){
                    return false;
                }
            }

            notifyLists.push(msg);
            return false;
        }
        
        return true;
    }

    /**
     * @public
     * 添加一条公告
     * 
     * @param {String} msg 公告内容
     */
    _proto.add = function (msg) {
        if(!this._UIInit){
            this.scrollRect = {x : 0, y : 0, width : this.width, height : this.height};
        }
        
        this._addItem(msg) && this._next(msg);
    }
})();